#include "execsql.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QDebug>
#include <stdio.h>
#include <QDateTime>

extern QString testerAccount;//声明外部变量

bool ExecSQL::addNewItem(const QString &testerAccount,const QString &computerSerial, const QString &naiya, const QString &dianzu,
                         const QString &loudian, const QString &naiyaDetails,const QString &dianzuDetails,const QString &loudianDetails, const QString &totalres)
{
    //测试总表数据插入
    QString current_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    qDebug() << "current_time" << current_time;
    //printf("%s\n",current_time.toLatin1().data());
    QSqlQuery query;
    QString sql = QString("INSERT INTO test_data_info (id, created_by, created_time, updated_by, updated_time, deleted_time, is_delete, sn_number, test_item, test_result, test_time) value(0, '%1',now(),'%1',now(),NULL,0,'%2','安规测试','%3','%4')")
            .arg(testerAccount).arg(computerSerial).arg(totalres).arg(current_time);
    printf("%s\n",sql.toLatin1().data());
    query.exec(sql);

    //交流耐压数据插入
    sql = QString("insert into test_data_details (id,created_by, created_time, updated_by, updated_time, deleted_time, is_delete, test_details_item, test_details_result, test_details_details, test_details_number, test_details_time, test_data_info_id) "
                  "value(0, '%1',now(),'%1',now(),NULL,0,'交流耐压测试','%2','%3','%1','%4',(SELECT id FROM test_data_info where sn_number = '%5' and created_by = '%1' AND test_item = '安规测试' and test_time='%6'))")
            .arg(testerAccount).arg(naiya).arg(naiyaDetails).arg(current_time).arg(computerSerial).arg(current_time);
    printf("%s\n",sql.toLatin1().data());
    query.exec(sql);

    //泄露电流数据插入
    sql = QString("insert into test_data_details (id,created_by, created_time, updated_by, updated_time, deleted_time, is_delete, test_details_item, test_details_result, test_details_details, test_details_number, test_details_time, test_data_info_id) "
                  "value(0, '%1',now(),'%1',now(),NULL,0,'泄露电流测试','%2','%3','%1','%4',(SELECT id FROM test_data_info where sn_number = '%5' and created_by = '%1' AND test_item = '安规测试' and test_time='%6'))")
            .arg(testerAccount).arg(loudian).arg(dianzuDetails).arg(current_time).arg(computerSerial).arg(current_time);
    printf("%s\n",sql.toLatin1().data());
    query.exec(sql);
    //接地电阻数据插入
    sql = QString("insert into test_data_details (id,created_by, created_time, updated_by, updated_time, deleted_time, is_delete, test_details_item, test_details_result, test_details_details, test_details_number, test_details_time, test_data_info_id) "
                  "value(0, '%1',now(),'%1',now(),NULL,0,'接地电阻测试','%2','%3','%1','%4',(SELECT id FROM test_data_info where sn_number = '%5' and created_by = '%1' AND test_item = '安规测试' and test_time='%6'))")
            .arg(testerAccount).arg(dianzu).arg(loudianDetails).arg(current_time).arg(computerSerial).arg(current_time);
    query.exec(sql);
    //查询数据是否插入成功
    sql = QString("SELECT id FROM test_data_info where sn_number = '%1' and created_by = '%2' AND test_item = '安规测试' and test_time='%3'").arg(computerSerial).arg(testerAccount).arg(current_time);;
    printf("%s\n",sql.toLatin1().data());
    query.exec(sql);
    return query.next();
}

bool ExecSQL::findUser(const QString account, const QString password)
{
    QSqlQuery query;
    QString sql = QString("select * from sys_user where user_name='%1' and user_password='%2'").arg(account).arg(password);
    qDebug() << sql;
    query.exec(sql);
    query.last();
    return query.isValid();
}
